package org.example.meetingsystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.meetingsystem.VO.CancellationRequestVO;
import org.example.meetingsystem.entity.CancellationRequest;
import org.example.meetingsystem.entity.User;
import org.example.meetingsystem.enums.CancellationStatus;
import org.example.meetingsystem.enums.UserRole;
import org.example.meetingsystem.annotation.RequireAuth;
import org.example.meetingsystem.mapper.CancellationRequestMapper;
import org.example.meetingsystem.service.CancellationService;
import org.example.meetingsystem.util.BusinessException;
import org.example.meetingsystem.util.ResponseUtil;
import org.example.meetingsystem.util.Result;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/cancellations")
@RequiredArgsConstructor
@Slf4j
@Tag(name = "取消预订管理")
public class CancellationController {

    private final CancellationRequestMapper cancellationRequestMapper;
    private final CancellationService cancellationService;

    /**
     * 查看待处理的取消申请
     */
    @GetMapping("/pending")
    @RequireAuth(roles = {UserRole.EMPLOYEE, UserRole.ADMIN})
    @Operation( summary ="查看待处理的取消申请")
    public Result<List<CancellationRequest>> getPendingRequests() {
        List<CancellationRequest> requests = cancellationRequestMapper.selectPendingRequests();
        return ResponseUtil.success(requests);
    }

    /**
     * 处理取消申请
     */
    @PutMapping("/{id}/process")
    @RequireAuth(roles = {UserRole.EMPLOYEE, UserRole.ADMIN})
    @Operation( summary ="处理取消申请")
    public Result<String> processCancellationRequest(
            @PathVariable @Parameter(ref = "申请ID") Long id,
            @RequestParam @Parameter(ref = "是否批准") Boolean approved,
            HttpServletRequest request) {

        User currentUser = (User) request.getAttribute("currentUser");

        try {
            cancellationService.processCancellationRequest(id, approved, currentUser.getId());
            return ResponseUtil.success(approved ? "取消申请已批准" : "取消申请已拒绝");
        } catch (BusinessException e) {
            return ResponseUtil.error(e.getMessage());
        }
    }

    /**
     * 查看所有取消申请记录
     */
    @GetMapping
    @RequireAuth(roles = {UserRole.EMPLOYEE, UserRole.ADMIN})
    @Operation( summary ="查看所有取消申请记录")
    public Result<IPage<CancellationRequestVO>> getAllCancellationRequests(
            @RequestParam(defaultValue = "1") @Parameter(description = "页码") Integer current,
            @RequestParam(defaultValue = "10") @Parameter(description = "每页数量") Integer size,
            @RequestParam(required = false) @Parameter(description = "申请状态") CancellationStatus status) {

        Page<CancellationRequestVO> page = new Page<>(current, size);

        // 使用新的方法查询带关联信息的数据
        IPage<CancellationRequestVO> result = cancellationRequestMapper.selectCancellationRequestsWithDetails(page, status);

        return ResponseUtil.success(result);
    }
}